package com.mlethe.library.fingerprint;

import android.app.Activity;

import androidx.annotation.NonNull;

import com.mlethe.library.fingerprint.callback.FingerprintCallback;
import com.mlethe.library.fingerprint.callback.IFingerprint;
import com.mlethe.library.fingerprint.entity.VerificationDialogStyle;
import com.mlethe.library.fingerprint.uitls.AndrVersionUtil;

/**
 * 指纹验证管理类
 *
 * @author Mlethe
 * @date 2019/7/9
 */
public class FingerprintVerifyManager {

    private IFingerprint fingerprint;
    ;

    public FingerprintVerifyManager(final Builder builder) {

        if (builder.cancelDialog) {
            if (AndrVersionUtil.isAboveAndrP()) {
                fingerprint = FingerprintImplForNoDialog.getInstance();
                fingerprint.setFingerprintCallback(builder.callback);
                fingerprint.authenticate(builder.context);
                return;
            } else if (AndrVersionUtil.isAboveAndrM()) {
                fingerprint = FingerprintImplForNoDialog.getInstance();
                fingerprint.setFingerprintCallback(builder.callback);
                fingerprint.authenticate(builder.context);
                return;
            } else {
                // Android 6.0 以下官方未开放指纹识别，某些机型自行支持的情况暂不做处理
                builder.callback.onHwUnavailable();
                return;
            }
        } else {
            if (AndrVersionUtil.isAboveAndrP()) {
                if (builder.enableAndroidP) {
                    fingerprint = FingerprintImplForAndrP.getInstance();
                } else {
                    fingerprint = FingerprintImplForAndrM.getInstance();
                }
            } else if (AndrVersionUtil.isAboveAndrM()) {
                fingerprint = FingerprintImplForAndrM.getInstance();
            } else {
                // Android 6.0 以下官方未开放指纹识别，某些机型自行支持的情况暂不做处理
                builder.callback.onHwUnavailable();
                return;
            }
        }
        // 检测指纹硬件是否存在或者是否可用，若false，不再弹出指纹验证框
        if (!fingerprint.canAuthenticate(builder.context)) {
            builder.callback.onHwUnavailable();
            builder.onDestroy();
            return;
        }
        /**
         * 设定指纹验证框的样式
         */
        // >= Android 6.0
        VerificationDialogStyle bean = new VerificationDialogStyle();
        bean.setCancelTextColor(builder.cancelTextColor);
        bean.setFingerprintColor(builder.fingerprintColor);

        // >= Android 9.0
        bean.setTitle(builder.title);
        bean.setSubTitle(builder.subTitle);
        bean.setDescription(builder.description);
        bean.setCancelBtnText(builder.cancelBtnText);
        fingerprint.setFingerprintCallback(builder.callback);
        fingerprint.setVerificationDialogStyle(bean);
        fingerprint.authenticate(builder.context);
    }

    /**
     * 获取指纹管理器
     *
     * @return
     */
    public IFingerprint getFingerprint() {
        return fingerprint;
    }

    /**
     * UpdateAppManager的构建器
     */
    public static class Builder {

        /**
         * 必选字段
         */
        private Activity context;
        private FingerprintCallback callback;

        /**
         * 可选字段
         */
        private int cancelTextColor;
        private int fingerprintColor;

        /**
         * 在Android 9.0系统上，是否开启google提供的验证方式及验证框
         */
        private boolean enableAndroidP;
        /**
         * 取消默认弹窗
         */
        private boolean cancelDialog;
        private String title;
        private String subTitle;
        private String description;
        /**
         * 取消按钮文字
         */
        private String cancelBtnText;

        /**
         * 构建器
         *
         * @param activity
         */
        public Builder(@NonNull Activity activity) {
            this.context = activity;
        }

        /**
         * 指纹识别回调
         *
         * @param callback
         */
        public Builder callback(@NonNull FingerprintCallback callback) {
            this.callback = callback;
            return this;
        }

        /**
         * 取消按钮文本色
         *
         * @param color
         */
        public Builder cancelTextColor(int color) {
            this.cancelTextColor = color;
            return this;
        }

        /**
         * 指纹图标颜色
         *
         * @param color
         */
        public Builder fingerprintColor(int color) {
            this.fingerprintColor = color;
            return this;
        }

        /**
         * 在 >= Android 9.0 系统上，是否开启google提供的验证方式及验证框
         *
         * @param enableAndroidP
         */
        public Builder enableAndroidP(boolean enableAndroidP) {
            this.enableAndroidP = enableAndroidP;
            return this;
        }

        /**
         * 取消弹窗
         *
         * @param cancelDialog
         */
        public Builder setCancelDialog(boolean cancelDialog) {
            this.cancelDialog = cancelDialog;
            return this;
        }

        /**
         * >= Android 9.0 的验证框的主标题
         *
         * @param title
         */
        public Builder title(String title) {
            this.title = title;
            return this;
        }

        /**
         * >= Android 9.0 的验证框的副标题
         *
         * @param subTitle
         */
        public Builder subTitle(String subTitle) {
            this.subTitle = subTitle;
            return this;
        }

        /**
         * >= Android 9.0 的验证框的描述内容
         *
         * @param description
         */
        public Builder description(String description) {
            this.description = description;
            return this;
        }

        /**
         * >= Android 9.0 的验证框的取消按钮的文字
         *
         * @param cancelBtnText
         */
        public Builder cancelBtnText(String cancelBtnText) {
            this.cancelBtnText = cancelBtnText;
            return this;
        }

        /**
         * 开始构建
         *
         * @return
         */
        public FingerprintVerifyManager build() {
            return new FingerprintVerifyManager(this);
        }

        private void onDestroy() {
            context = null;
            callback = null;
            title = null;
            subTitle = null;
            description = null;
            cancelBtnText = null;
        }
    }

}
